package study.javabasic.communication.WhileCom;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangshuhan on 2019/3/20.
 */
public class Test {
    public static List<String> os = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
         List<String>   list = new ArrayList<>();

        Thread b = new Thread(new ThreadB(list), "B");
        b.start();
        Thread a = new Thread(new ThreadA(list), "A");
        a.start();

        Thread t1 = new Thread() {

            @Override
            public void run() {
                System.out.println("线程1启动");
                for (int i = 0; i < 10; i++) {
                    os.add("aa");
                    System.out.println("添加一个元素");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程1完成任务退出");
            }

        };
        Thread t2 = new Thread() {

            @Override
            public void run() {
                System.out.println("线程2启动");
                while (true) {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(os.size());
                    if (os.size() == 5) {
                        System.out.println("执行线程2业务");
                        break;
                    }
                }
                System.out.println("线程2完成任务退出");
            }

        };
        t1.start();
        t2.start();

    }
}
